
#include "SmartMaterialInstanceExtensions.h"
//#include "Map.h"
#include "Engine/Texture.h"
#include "Materials/MaterialInstance.h"
#include "Materials/MaterialInstanceConstant.h"
#include "Materials/MaterialExpressionTextureBase.h"
//#include "SmartMaterialInstanceExtensions.h"

void USmartMaterialInstanceExtensions::SmSetTextures(UMaterialInstanceConstant * MaterialInstance, TMap<FString, FString> Data)
{
	TArray < FTextureParameterValue > SrcTextureParameterValues = MaterialInstance->TextureParameterValues;
	TArray < FTextureParameterValue > TagTextureParameterValues;
	bool bIsUpdate = false;
	for (FTextureParameterValue TextureParameterValue : SrcTextureParameterValues) {
		if (Data.Contains(TextureParameterValue.ParameterInfo.Name.ToString())) {
			FString TexturePath = Data[TextureParameterValue.ParameterInfo.Name.ToString()];
			UTexture* Texture = Cast<UTexture>(StaticLoadObject(UTexture::StaticClass(), NULL, *TexturePath));
			if (Texture) {
				TextureParameterValue.ParameterValue = Texture;
				bIsUpdate = true;
			}
		}
		TagTextureParameterValues.Add(TextureParameterValue);
	}
	if (bIsUpdate) {
		MaterialInstance->TextureParameterValues = TagTextureParameterValues;
	}
}

TMap<FString, FString> USmartMaterialInstanceExtensions::SmListTextures(UMaterialInstanceConstant * MaterialInstance)
{
	TMap<FString, FString> MaterialTextures;
	TArray < FTextureParameterValue > SrcTextureParameterValues = MaterialInstance->TextureParameterValues;
	for (FTextureParameterValue TextureParameterValue : SrcTextureParameterValues) {
		if (!MaterialTextures.Contains(TextureParameterValue.ParameterInfo.Name.ToString())) {
			MaterialTextures.Add(TextureParameterValue.ParameterInfo.Name.ToString(), TextureParameterValue.ParameterValue->GetPathName());
		}
	}
	return MaterialTextures;
}
